package com.lgh.ibs.plugins.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import com.lgh.ibs.log.IbsLogger;
import com.lgh.ibs.plugins.views.IbsResourceView;
import com.lgh.ibs.plugins.views.ProjectImpView;
import com.lgh.ibs.plugins.views.ProjectVersionView;
import com.lgh.ibs.plugins.views.RootChildrenVersionView;

/**
 * @ClassName: MainMenuHandler 
 * @Description: IBS菜单事件处理器
 * @date 2016年12月26日 下午1:20:38 
 * @author guohao.li
 */
public class MainMenuHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		try {
			
			String commandId = event.getCommand().getId();
			if("com.lgh.ibs.plugins.commands.projectVersion".equals(commandId)){//工程版本号菜单
				openIbsView(event, ProjectVersionView.ID);
			}else if("com.lgh.ibs.plugins.commands.branchesCodeImp".equals(commandId)){//分支导入菜单
				openIbsView(event, ProjectImpView.ID);
			}else if("com.lgh.ibs.plugins.commands.resourcesView".equals(commandId)){//资源管理视图
				openIbsView(event, IbsResourceView.ID);
			}else if("com.lgh.ibs.plugins.commands.rootChildrenVersion".equals(commandId)){//Root子分支版本号修改
				openIbsView(event, RootChildrenVersionView.ID);
			} else{
				shwoInfo(event);
			}
		} catch (Exception e) {
			e.printStackTrace();
			IbsLogger.ERROR(e.getMessage(),e);
		}
		return null;
	}
	
	public void shwoInfo(ExecutionEvent event){
		IWorkbench workbench = PlatformUI.getWorkbench();
		IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
		MessageDialog.openInformation(window.getShell(),"友情提示","功能即将开放，敬请期待！");
	}
	
	/**
	 * 打开IBS视图
	 */
	public void openIbsView(ExecutionEvent event,String viewId){
		try {
			IWorkbench workbench = PlatformUI.getWorkbench();
			IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
			IWorkbenchPage page = workbenchWindow.getActivePage();
			page.showView(viewId);
			
			/*IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			activePage.showView(viewId, "1", IWorkbenchPage.VIEW_ACTIVATE);*/
		} catch (Exception e) {
			e.printStackTrace();
			IbsLogger.ERROR(e.getMessage(),e);
		}
	}
	
	public String showInoutDialog(){
		InputDialog inputDialog =new InputDialog(
				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
				"Example",
				"Enter a value",
				"name",
				null);
		if(inputDialog.open()== InputDialog.OK){
			return inputDialog.getValue();
		}
		return null;
		
	}
	
//	Display display = PlatformUI.createDisplay();
//	PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
}
